var listPage = avalon.define({
	$id: "listPage",
	uid: "",
	noData:"",
	tasks: []
});

mui.ready(function() {
	var pageNo = 1;
	var uid = $("#uid").val();
	listPage.uid = uid;
	var mui_task = mui('#task-list');
	mui.init({
		pullRefresh: {
			container: "#task-list",
			up: {
				auto: true,
				contentrefresh: $.i18n.contentRefresh,
				contentnomore: $.i18n.noData,
				callback: function() {
					listPage.noData="";
					setTimeout(function() {
						mui.ajax('/task/list.json', {
							data: {
								uid: uid,
								pageNo: pageNo
							},
							dataType: 'json',
							success: function(items) {
								var tasks = listPage.tasks;
								avalon.each(items, function(i, item) {
									var status = status = item.status;
									var clazz = "";
									if(0 == status) {
										status = "Pending";
										clazz = "mui-badge-royal";
									} else if(1 == status) {
										status = "Approved";
										clazz = "mui-badge-green";
									} else if(2 == status) {
										status = "Rejected";
										clazz = "mui-badge-red";
									} else if(3 == status) {
										status = "Disabled";
										clazz = "";
									}
									item.status = "<span class=\"mui-badge " + clazz + "\">" + status + "</span>";
									tasks.push(item);
								});
								listPage.tasks = tasks;
								mui_task.pullRefresh().endPullupToRefresh(false);
								
								if(listPage.tasks.length==0){
									listPage.noData="<img src='../../resources/images/noDataImg.png'><p>No data</p>";
								}
								
							}
						});
						pageNo = pageNo + 1;

					}, window.DAHUA_CONFIG.LOADING_TIME);

				}
			}
		}
	});
	
	
	mui(".mui-table-view").on('tap', 'a', function() {
		var url = this.getAttribute('href');
		mui.openWindow({
			url: url 
		});
	});

});